<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    table,
    tr,
    th,
    td {
      border: 1px solid #666;
    }

    table {
      width: 80%;
      margin: 50px auto 0;
      border-collapse: collapse;
    }

    .select-state {
      display: inline-block;
      width: 14px;
      height: 14px;
      background-color: #999;
    }

    .selected {
      background-color: #2994eb;
    }
  </style>
</head>

<body>
  <div id="app">
    <table>
      <tr>
        <th>
          <span class="select-state" :class="{selected: isSelectedAll}" @click="changeAll(isSelectedAll)"></span>
        </th>
        <th>商品名</th>
        <th>价格</th>
        <th>数量</th>
        <th>小计</th>
        <th>操作</th>
      </tr>
      <tr v-for="item in goods" :key="item.goodsId">
        <td><span class="select-state" :class="{selected: item.selected}" @click="changeSelected(item)"></span></td>
        <td>{{item.goodsName}}</td>
        <td>{{item.price}}</td>
        <td>
          <button @click="decrement(item)">-</button>
          <input type="text" v-model="item.num">
          <button @click="increment(item)">+</button>
        </td>
        <td>{{item.price * item.num}}</td>
        <td>
          <a href="" @click.prevent="delGoods(item.goodsId)">删除</a>
        </td>
      </tr>
    </table>
    <div>
      <p>选择数量：{{getTotal.totalCount}}</p>
      <p>总价：{{getTotal.totalPrice}}</p>
    </div>
  </div>

  <script src="https://lib.baomitu.com/vue/2.6.14/vue.js"></script>
  <script>
    // 模拟数据
    const goods = [
      {
        goodsId: 1,
        goodsName: '小米10',
        price: 5999,
        num: 1
      },
      {
        goodsId: 2,
        goodsName: '华为 Mate 50',
        price: 7999,
        num: 1
      },
      {
        goodsId: 3,
        goodsName: '苹果14',
        price: 6999,
        num: 1
      }
    ];

    let vm = new Vue({
      el: '#app',
      data: {
        goods: [] // 有默认值
      },
      mounted() {
        // 请求数据并改造数据
        this.goods = goods.map(item => {
          item.selected = true;
          return item;
        });
      },
      computed: {
        isSelectedAll() {
          return this.goods.every(item => item.selected)
        },
        getTotal() {
          let totalPrice = 0;
          let totalCount = 0;

          for (let i = 0; i < this.goods.length; i++) {
            if (this.goods[i].selected) {
              totalPrice += this.goods[i].price * this.goods[i].num;
              totalCount += this.goods[i].num;
            }
          }
          return {
            totalCount,
            totalPrice
          }
        }
      },
      methods: {
        decrement(item) {
          if (item.num > 1) {
            item.num -= 1;
          }
        },
        increment(item) {
          item.num += 1;
        },
        delGoods(id) {
          // this.goods = this.goods.filter(item => item.goodsId != id);
          this.goods = this.goods.filter(function (item) {
            return item.goodsId != id
          });
        },
        changeSelected(item) {
          item.selected = !item.selected;
        },
        changeAll(isSelectedAll) {
          this.goods = this.goods.map(item => {
            item.selected = !isSelectedAll;
            return item;
          })
        }
      }
    })
  </script>
</body>

</html>